import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

public class CreateJson {
    public static void main(String[] args) {
        initJson(JSF_SERVICE_NAME, JSF_ALIAS, methodList);
        writeFile(jsonAll);
        System.out.print("生成json文件成功");
    }

    private static String TARGET_PATH = "/Users/zhang/work/addWastageDetailForRf.json";//目标文件路径
//        private static String JSF_SERVICE_NAME = "com.jd.jcloud.wms.inventory.wastage.service.WastageHeaderService";//接口服务名,整单提交
    private static String JSF_SERVICE_NAME = "com.jd.jcloud.wms.inventory.wastage.service.WastageDetailService";//接口服务名,损耗详细提交
    private static String methodList[][] = {
//            {"整单提交", "submitWastageForRf"},
            {"损耗详细提交", "addWastageDetailForRf"},
//            {"执行单位", "queryPalletStandardQuantityBySkuForApp"},
//            {"库存调整切换容器（满箱）", "switchingContainers4App"},
//            {"库存调整校验储位与容器", "checkToLocaitonOrContauner4App"},
//            {"库存调整提交任务", "sumbitAdjustmentStockChangeDetail4App"},
//            {"库存调整任务明细列表", "queryStockChangeDetailList4App"},
//            {"库存调整任务主档列表", "queryStockChangeListPage4App"},
//            {"库存调整查询是否存在调整任务", "getReceived4App"},
//            {"库存调整查询大区数量", "getAreaCount"}
    };
    private static String GROUP_NAME = "jd_cw";//物流网关业务分组
    private static String SERVICE_DOMAIN = "jwms.jclps.com";//物流网关服务域
    //    private static String JSF_ALIAS[] = {"test"};//云仓助手测试环境别名
    private static String JSF_ALIAS[] = {"jcloud-wms-gray", "clps-jwms-v0.1", "jwms-yc3-v0.1", "jcloud-wms-v0.2", "jcloud-wms-v0.3"};//云仓助手线上环境别名
    private static String isAnonymous = "false";//是否允许匿名访问

    private static StringBuffer jsonAll = new StringBuffer();

    protected static void initJson(String jsf_service_name, String jsf_alias[], String methodList[][]) {
        jsonAll.append("[");

        for (int j = 0; j < jsf_alias.length; j++) {
            for (int i = 0; i < methodList.length; i++) {
                String cnName = methodList[i][0];
                String enName = methodList[i][1];
                if ("".equals(cnName)) {
                    cnName = enName;
                }
                jsonAll.append(getJsonString(cnName, enName, jsf_alias[j], jsf_service_name, SERVICE_DOMAIN));
            }
        }

        jsonAll.deleteCharAt(jsonAll.length() - 1);
        jsonAll.append("]");
    }

    private static void writeFile(StringBuffer sb) {
        //写入文件
        File f = new File(TARGET_PATH);//新建文件
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(f));
//            String result=new String(sb.toString().getBytes(),"utf-8");
            bw.write(sb.toString());
            bw.flush();
            bw.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    private static String getJsonString(String methodNameChinese, String methodName, String alias, String interfaceName, String serviceDomain) {
        String intercept = interfaceName.substring(interfaceName.lastIndexOf(".") + 1);
        return "{\"api\":{\"defaultClusterId\":0,\"domain\":\"" + serviceDomain + "\",\"groupCode\":\"" + GROUP_NAME + "\",\"hasSensitiveData\":false,\"isAnonymous\":" + isAnonymous + ",\"isNeedAuth\":false,\"name\":\"" + methodNameChinese + "\",\"routingId\":\"/" + intercept + "/" + alias + "/" + methodName + "\",\"type\":1,\"url\":\"/" + intercept + "/" + alias + "/" + methodName + "\"},\"clusters\":[{\"connectTimeout\":500,\"defaultCluster\":true,\"domain\":\"" + serviceDomain + "\",\"groupCode\":\"" + GROUP_NAME + "\",\"name\":\"" + alias + ":" + methodName + "\",\"readTimeout\":5000,\"reliveTimeout\":10000,\"routers\":\"{\\\"jsf_method\\\":\\\"" + methodName + "\\\",\\\"ctx_write\\\":[],\\\"args_rules\\\":[],\\\"fill_brakt\\\":false,\\\"token\\\":\\\"\\\",\\\"jsf_alias\\\":\\\"" + alias + "\\\",\\\"type\\\":\\\"JSF_ST\\\",\\\"jsf_iface\\\":\\\"" + interfaceName + "\\\",\\\"version\\\":\\\"v2\\\"}\",\"sendTimeout\":5000,\"type\":1,\"updatePolicy\":1}],\"domainServers\":[]},";
    }
}
